package com.csii.pos.caacctewsu;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.pos.caacctewsu.service.ICaAcctEwSuService;
import com.csii.pos.caacctewsu.vo.CaAcctEwVo;
import com.csii.pos.dto.caacctew.CaAcctEwReq;
import com.csii.pos.dto.caacctew.CaAcctEwRes;
import com.csii.pos.posplan.PosPlan;
import com.csii.pos.rpc.Paging;
import com.csii.pos.service.caacctew.CaAcctEwService;
import com.csii.pos.tsysparam.TSysParam;
import com.csii.pos.tsysparam.service.TSysParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:
 * @create:
 */
@RestController
@Slf4j
public class CaAcctEwController implements CaAcctEwService {

    @Autowired
    private ICaAcctEwSuService caAcctEwSuService;
    @Autowired
    private TSysParamService tSysParamService;
    /** 
     * @description:  账户限额查询外联接口
     * @param: caAcctEwReq 
     * @return:  CaAcctEwRes
     * @author liuzhonghua
     * @date:  2021-7-23
     */ 
    @Override
    public CaAcctEwRes queryAcctLimtAmt(CaAcctEwReq caAcctEwReq) {
        QueryWrapper<CaAcctEwSu> queryWrapper = new QueryWrapper<>();
        Page<CaAcctEwSu> page = new Page<>(caAcctEwReq.getPageNo(),caAcctEwReq.getPageSize());
        //传入数据不为null，进行数据转换
        if (caAcctEwReq!=null) {
            CaAcctEwSu caAcctEwSu = ConvertUtils.convert(CaAcctEwSu.class, caAcctEwReq);
            if (!StringUtils.isEmpty(caAcctEwSu.getEwAcctNo())) {
                queryWrapper.like("EW_ACCT_NO", caAcctEwSu.getEwAcctNo());
            }
            if (!StringUtils.isEmpty(caAcctEwSu.getBankCode())) {
                queryWrapper.like("BANK_CODE", caAcctEwSu.getBankCode());
            }
        }
        IPage<CaAcctEwSu> iPage = caAcctEwSuService.page(page,queryWrapper);

        CaAcctEwRes caAcctEwVos = changeVo(iPage);

        return caAcctEwVos;
    }
    /** 
    * @description:  数据转换
    * @param: list 
    * @return:  list
    * @author liuzhonghua
    * @date:  2021-7-23
    */ 
    private CaAcctEwRes changeVo(IPage<CaAcctEwSu> list){

        //返回的list
        List<CaAcctEwVo> lists = new ArrayList<>();
        //循环存入数据
        for (CaAcctEwSu c:list.getRecords()) {
            QueryWrapper<TSysParam> queryWrapper = new QueryWrapper<>();
            CaAcctEwVo caAcctEwVo = ConvertUtils.convert(CaAcctEwVo.class, c);
            caAcctEwVo.setUnit("万元");
            queryWrapper.eq("CODE",c.getBankCode());
            Map<String, Object> map = tSysParamService.getMap(queryWrapper);
            caAcctEwVo.setBankEnFs(map.get("VALUE").toString());
            lists.add(caAcctEwVo);
        }
        CaAcctEwRes caAcctEwRes = ConvertUtils.convert(CaAcctEwRes.class, list);
        caAcctEwRes.setCode("200");
        caAcctEwRes.setData(lists);
        Paging paging = new Paging();
        paging.setTotal(list.getTotal());
        paging.setPageNo(list.getCurrent());
        paging.setPageSize(list.getSize());
        Object o = JSONArray.toJSON(paging);
        String string = o.toString();
        caAcctEwRes.setJson(string);
        System.out.println(string);
        return caAcctEwRes;
    }
}
